<?php

class UserController extends BaseController
{
	public function index()
	{
		$this->view('all');
		
	}

	public function view($name = null)
	{
		$this->registry->template->setMenuKey('user');
		$tem = $this->registry->template;
		
		if ($name == 'all') {
			
			$tags['manuContentTitle'] = 'User Management';
			$content = $this->getUserTable();
		}
		else {
			$tags['manuContentTitle'] = 'View User: ' . $name;
			$atts = array('USERNAME', 'ACCOUNT_STATUS', 'LOCK_DATE', 'CREATED', 'DEFAULT_TABLESPACE', 'TEMPORARY_TABLESPACE');
			$content = $this->getGeneralTable($name, $atts, 'user') . '</br>' .
					   $this->getRoleTbl($name) . '</br>' .
					   $this->getSysPrivTbl($name) . '</br>' .
					   $this->getObjPrivTbl($name) . '</br>' .
				       $this->getQuotaTbl($name);
		}

		$tags['path'] = MANU_ROOT_PATH . 'user';
		$tags['what'] = 'user';
		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);
				
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
		
		$output = $this->registry->template->getPageContent('index', 'Oracle User Manager', $content, $tags);

		echo $output;
		
	}

	public function add()
	{
		$this->registry->template->setMenuKey('user');
		
		$tem = $this->registry->template;
		$man = $this->registry->db->get('userManager');
		$tags['manuContentTitle'] = 'Add User';
		
		$method = $_SERVER['REQUEST_METHOD'];
		
		if ($method == 'POST') {
			if(isset($_POST["username"])) {
				$username = strip_tags($_POST["username"]);
				if ($username == "") {
					header("Location: ../add/username");
					exit();
				}
			}
			if(isset($_POST["password"])) {
				$password = strip_tags($_POST["password"]);
				if ($password == "") {
					header("Location: ../add/password");
					exit();
				}
			}
			if(isset($_POST["defTablespace"])) {
				$defTbl = strip_tags($_POST["defTablespace"]);
				if ($defTbl == "") {
					header("Location: ../add/defTablespace");
					exit();
				}
			}
			if(isset($_POST["tempTablespace"])) {
				$tempTbl = strip_tags($_POST["tempTablespace"]);
				if ($tempTbl == "") {
					header("Location: ../add/tempTablespace");
					exit();
				}
			}
			if(isset($_POST["quota"])) {
				$quota = strip_tags($_POST["quota"]);
				if ($quota == "") {
					header("Location: ../add/quota");
					exit();
				}
			}

			if(isset($_POST["profile"])) {
				$profile = strip_tags($_POST["profile"]);
				if ($profile == "") {
					header("Location: ../add/profile");
					exit();
				}
			}
			
			$man->addUser($username, $password, $defTbl, $tempTbl, $quota, $profile);
			
			header("Location: ./index");
			
			exit();
		} 	
		else {
			$create = $man->checkPriv('CREATE USER');
			if($create == false){
				$content = <<<EOT
User don't have CREATE USER Privilege !<br /><br />
EOT;
			}else{
				// get temporary_tablespace names
				$tags['user_val'] = "";
				$tags['pass_val'] = "";
				$tags['quo_val'] = 0;
				$tags['disable'] = null;
				$names = $man->getTempTablespaceName();
				$str = '<option value="{value}">{value}</option>';
				$tags['manuTemOption'] = '<option value="0">Choose Temporary Tablespace</option>';
				foreach ($names as $name) {
					$tags['manuTemOption'] .= str_replace('{value}', $name->get('TEMPORARY_TABLESPACE'), $str);
				}
				
				// get default_tablespace names
				$names = $man->getDefTablespaceName();
				$str = '<option value="{value}">{value}</option>';
				$tags['manuDefOption'] = '<option value="0">Choose Default Tablespace</option>';
				foreach ($names as $name) {
					$tags['manuDefOption'] .= str_replace('{value}', $name->get('DEFAULT_TABLESPACE'), $str);
				}
				
				// get profile names
				$names = $man->getProfileName();
				$str = '<option value="{value}">{value}</option>';
				$tags['manuProfOption'] = '<option value="0">Choose Profile</option>';
				foreach ($names as $name) {
					$tags['manuProfOption'] .= str_replace('{value}', $name->get('PROFILE'), $str);
				}
	
				$content = $tem->getModule('adduser', $tags);
			}

		}
		
		$tags['path'] = MANU_ROOT_PATH . 'user';
		
		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);
			
				
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
		
		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);
		
		echo $output;
	}
	
	public function edit($namePara)
	{
		$this->registry->template->setMenuKey('book');
		
		$tem = $this->registry->template;
		$man = $this->registry->db->get('userManager');
		$tags['manuContentTitle'] = 'Edit User';
		
		$method = $_SERVER['REQUEST_METHOD'];
		
		if ($method == 'POST') {
			$username = null;
			if(isset($_POST["username"])) {
				$username = strip_tags($_POST["username"]);				
			}
			$password = null;
			if(isset($_POST["password"])) {
				$password = strip_tags($_POST["password"]);
			}
			$defTbl = null;
			if(isset($_POST["defTablespace"])) {
				$defTbl = strip_tags($_POST["defTablespace"]);				
			}
			$tempTbl = null;
			if(isset($_POST["tempTablespace"])) {
				$tempTbl = strip_tags($_POST["tempTablespace"]);				
			}
			$quota = null;
			if(isset($_POST["quota"])) {
				$quota = strip_tags($_POST["quota"]);				
			}
			$account = null;
			if(isset($_POST["account"])) {
				$account = strip_tags($_POST["account"]);			
			}
			$profile = null;
			if(isset($_POST["profile"])) {
				$profile = strip_tags($_POST["profile"]);				
			}
			
			$man->editUser($namePara, $password, $defTbl, $tempTbl, $quota, $account, $profile);
			
			header("Location: ../index");
			
			exit();
		} 	
		else {
			// get temporary_tablespace names
			$user = $man->getBasicInfo($namePara);
			$quotaInfo = $man->getQuotaFrom($namePara); 
			$tags['user_val'] = $user[0]->get(strtoupper('username'));
			$tags['pass_val'] = null;
			$tags['disable'] = "disabled=\"disabled\"";
			if($user[0]->get(strtoupper('account_status')) == 'OPEN'){
				$tags['lock'] = null;
				$tags['unlock'] = "selected=\"selected\"";
			}else{
				$tags['lock'] = "selected=\"selected\"";
				$tags['unlock'] = null;
			}
				
			if(count($quotaInfo) != 0)
				$tags['quo_val'] = $quotaInfo[0]->get(strtoupper('max_bytes'));
			else 
				$tags['quo_val'] = 0;
			$names = $man->getTempTablespaceName(); 
			$str = '<option value="{value}">{value}</option>';
			$selected = '<option value="{value}" selected="selected" >{value}</option>';
			$tags['manuTemOption'] = '';
			foreach ($names as $name) {
				if($name->get('TEMPORARY_TABLESPACE') == $user[0]->get(strtoupper('temporary_tablespace')))
					$tags['manuTemOption'] .= str_replace('{value}', $name->get('TEMPORARY_TABLESPACE'), $selected);
				else
					$tags['manuTemOption'] .= str_replace('{value}', $name->get('TEMPORARY_TABLESPACE'), $str);
						
			}
			
			// get default_tablespace names
			$names = $man->getDefTablespaceName();
			$str = '<option value="{value}">{value}</option>';
			$tags['manuDefOption'] = '';
			foreach ($names as $name) {
				if($name->get('DEFAULT_TABLESPACE') == $user[0]->get(strtoupper('default_tablespace')))
					$tags['manuDefOption'] .= str_replace('{value}', $name->get('DEFAULT_TABLESPACE'), $selected);
				else
					$tags['manuDefOption'] .= str_replace('{value}', $name->get('DEFAULT_TABLESPACE'), $str);
				
			}
			
			// get profile names
			$names = $man->getProfileName();
			$str = '<option value="{value}">{value}</option>';
			$tags['manuProfOption'] = '';
			foreach ($names as $name) {
				if($name->get('PROFILE') == $user[0]->get(strtoupper('profile')))
					$tags['manuProfOption'] .= str_replace('{value}', $name->get('PROFILE'), $selected);
				else
					$tags['manuProfOption'] .= str_replace('{value}', $name->get('PROFILE'), $str);
				
			}

			$content = $tem->getModule('edituser', $tags);
		}
		
		$tags['path'] = MANU_ROOT_PATH . 'user';
		
		if (isset($_SESSION['message'])) {
			$tags['manuMessage'] = ($_SESSION['message'] == 'success') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);   
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
		
		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);
		
		echo $output;
	}
	
	
	public function getUserTable()
	{
		$user  = $this->registry->db->get('userManager')->getCurrentUser();
		$privMan = $this->registry->db->get('privilegeManager');

		//some basic info showed
		$atts = array('USERNAME', 'ACCOUNT_STATUS', 'LOCK_DATE', 'CREATED', 'DEFAULT_TABLESPACE', 'TEMPORARY_TABLESPACE');
		
		if ($privMan->hasDBA($user)) {
			$rows = $this->registry->db->get('userManager')->getBasicInfo();
		}
		else {
			$rows = $this->registry->db->get('userManager')->getBasicInfo($user);
		}		 					
		
		$res = $this->getEntityTable($atts, $rows, 'user');

		return $res;
	}

	/*
	 * @description:	Get Role Table
	 *
	 */
	public function getRoleTbl($name)
	{
		$man = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;

		$atts = array('GRANTED_ROLE', 'ADMIN_OPTION');
		$rows = $man->getRoleFrom($name);

		$res = '<div id="title">Roles</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	/*
	 * @description:	Get System Privilege Table
	 *
	 */
	public function getSysPrivTbl($name)
	{
		$man = $this->registry->db->get('privilegeManager');
		$tem = $this->registry->template;

		$atts = array('PRIVILEGE', 'ADMIN_OPTION');
		$rows = $man->getSysPriv($name);

		$res = '<div id="title">System Privileges</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	/*
	 * @description:	Get Object Privilege Table
	 *
	 */
	public function getObjPrivTbl($name)
	{
		$man = $this->registry->db->get('privilegeManager');
		$tem = $this->registry->template;

		$atts = array('PRIVILEGE', 'TABLE_NAME', 'OWNER', 'GRANTOR', 'GRANTABLE');
		$rows = $man->getObjPriv($name);

		$res = '<div id="title">Object Privileges</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	/*
	* @description:	Get Quota Table
	*
	*/
	public function getQuotaTbl($name)
	{
		$man = $this->registry->db->get('userManager');
		$tem = $this->registry->template;
		
		$atts = array('TABLESPACE_NAME', 'BYTES', 'MAX_BYTES', 'BLOCKS', 'MAX_BLOCKS', 'DROPPED');
		$rows = $man->getQuotaFrom($name);
		
		$res = '<div id="title">Quotas</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);
		
		return $res;
	}
	
	/*
	 * @description:	Delete user
	 */
	public function delete($username)
	{
		$man = $this->registry->db->get('userManager');
		$tem = $this->registry->template;
		
		$drop = $man->checkPriv('DROP USER');
		if($drop == false){
			$content = <<<EOT
User don't have DROP USER Privilege !<br /><br />
EOT;
			$tags['manuContentTitle'] = 'Drop User';
			$tags['manuMessage'] = '';
			
			$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);
			
			echo $output;
		}
		else { 
			$man->deleteUser($username);
			header("Location: ../index");
			exit();
		}					
	}
}

?>